// pages/tabBar/my/order/order.js
import {
    _headerPost,
    _headerGet,
    config,
} from '../../../../utils/httpUtils.js'
const RSA = require("../../../../utils/wx_rsa.js");
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        navbarData: {
            showCapsule: 1, //是否显示左上角图标
            title: '我的订单' //导航栏 中间的标题
        },
        height: app.globalData.height * 2 + 20,
        itemOrder: [],
        page: 1,
        selectId: 0,
        butList: [{
                key: 0,
                name: '全部'
            }, {
                key: 1,
                name: '待付款'
            },
            // {
            //     key: 2,
            //     name: '待使用'
            // },
            {
                key: 3,
                name: '待发货'
            }, {
                key: 4,
                name: '待收货'
            },
            // {
            //     key: 5,
            //     name: '待评价'
            // },
            // {
            //     key: 6,
            //     name: '退款/售后'
            // }
        ]
    },
    onLoad() {
        this.getList(0, 1)

    },
    onShow() { //每次进入页面刷新
        this.getList(this.data.selectId, this.data.page)
    },
    getList(type, pageNum) {
        // console.log(`订单类型:${type},页码 ${pageNum}`)
        _headerGet('/api/order/order_list', {
            c_client: "weixinSmall",
            page: pageNum,
            state: type
        }, res => {
            if (res.data.code == 1) {
                if (pageNum == 1) {
                    this.setData({
                        itemOrder: res.data.data,
                        page: pageNum
                    })
                } else {
                    this.setData({
                        itemOrder: [...this.data.itemOrder, ...res.data.data],
                        page: pageNum
                    })
                }

            } else {
                wx.showToast({
                    title: '没有更多数据了',
                    duration: 2000,
                    icon: 'none'
                })
            }
        })
    },
    //去首页
    goShopping() {
        wx.switchTab({
            url: '/pages/tabBar/specialty/specialty',
        })
    },
    // 判断点击类型
    handleOrder(e) {
        let data = e.target.dataset
        let body = data.body
        // console.log(e.target.dataset)

        switch (body.o_state) {
            case 0:
                if (data.type == 1) this.closeOrder(body.o_id, data.idx)
                else this.goToPay(body)
                break
            case 1:
                if (body.o_type == 'area')
                    this.lookWriteOff(body.o_verify_code)
                else if (body.oe_id == 0)
                    wx.showToast({
                        title: '提醒发货成功'
                    })
                else {
                    if (data.type == 1) {
                        this.lockExpress(body.o_id) //查看物流
                    } else {
                        wx.showModal({
                            title: '提示',
                            content: '您确定要确认收货？',
                            success: res => {
                                if (res.confirm)
                                    this.confirmGoods(body.o_id, data.idx) //确认收货  
                            }
                        })
                    }
                }
                break
            case 2:
                if (data.type == 1) {} else {}
                break
            case 3:
                console.log('已评价')
                break
            case 4:
                if (body.o_goods.length > 1)
                    wx.navigateTo({
                        url: '../order-detail/index?orderId=' + body.o_id,
                    })
                else if (body.orec_id > 0 && body.oref_state > 0) { // 查看详情
                    wx.navigateTo({
                        url: '../order-detail/index?orderId=' + body.o_id,
                    })
                } else if (body.orec_id == 0 && body.oref_state > 0) { // 评价
                    if (data.type == 1) this.lookSale(body)
                    else this.Evaluate(body)
                } else if (body.orec_id > 0 && body.oref_state == 0) { // 售后
                    this.afterSale(body)
                } else if (body.orec_id == 0 && body.oref_state == 0) { // 可售后，可评价
                    if (data.type == 1) {
                        this.afterSale(body)
                    } else
                        // this.Evaluate(body)  // 目前没有评价 转售后了
                        this.call()
                }
                break
            default:
                wx.navigateTo({
                    url: '../order-detail/index?orderId=' + body.o_id,
                })
                break
        }
    },
    //去支付
    goToPay(order) {
        let _data = {
            order_id: RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt(`${order.o_id}`)),
            c_client: RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt("weixinSmall")),
            type: RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt("order"))
        }
        _headerPost('/api/Order/commit_order', _data, res => {
            if (res.data.code == 1) {
                wx.navigateTo({
                    url: '/pages/public/topay/index?payInfo=' + JSON.stringify(res.data.data)
                })
            } else {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none'
                })
            }
        })

    },
    //取消订单
    closeOrder(id, idx) {
        _headerPost('/api/order/order_del', {
            order_id: RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt(`${id}`)),
            c_client: RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt("weixinSmall"))
        }, res => {
            if (res.data.code == 1) {
                this.data.itemOrder.splice(idx, 1)
                this.setData({
                    itemOrder: this.data.itemOrder
                })
                wx.showModal({
                    title: '提示',
                    content: '取消成功',
                    showCancel: false
                })
            } else {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none'
                })
            }
        })
    },
    //确认收货
    confirmGoods(id, idx) {
        _headerPost('/api/order/order_receiving', {
            id: RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt(`${id}`)),
            c_client: RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt("weixinSmall"))
        }, res => {
            if (res.data.code == 1) {
                this.data.itemOrder.splice(idx, 1)
                this.setData({
                    itemOrder: this.data.itemOrder
                })
                wx.showModal({
                    title: '提示',
                    content: '收货成功',
                    showCancel: false
                })
            } else {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none'
                })
            }
        })
    },
    // 售后打电话
    call() {
        wx.makePhoneCall({
            phoneNumber: '400-165-6655',
        })
    },
    //订单详情
    goToDetail(e) {
        let orderId = e.currentTarget.id
        wx.navigateTo({
            url: '../order-detail/index?orderId=' + orderId,
        })
    },
    //查看物流
    lockExpress(id) {
        // console.log(id)
        wx.navigateTo({
            url: `/pages/public/logistics/index?id=${id}`,
        })
    },
    //顶部菜单选择
    tabClick(e) {
        this.setData({
            selectId: e.currentTarget.id,
            itemOrder: []
        })
        this.getList(e.currentTarget.id, 1)
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        this.getList(this.data.selectId, this.data.page + 1)
    }
})